package acm.天梯赛练习2;

import java.util.Scanner;

public class E {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int T = input.nextInt();
            while (T-- > 0) {
                int n = input.nextInt();
                int[][] magicSquare = new int[n][n];
                int i = n / 2;
                int j = n - 1;
                for (int num = 1; num <= n * n; ) {
                    if (i == -1 && j == n) {
                        j = n - 2;
                        i = 0;
                    } else {
                        if (j == n)
                            j = 0;
                        if (i < 0)
                            i = n - 1;
                    }
                    if (magicSquare[i][j] != 0) {
                        j -= 2;
                        i++;
                        continue;
                    } else
                        magicSquare[i][j] = num++;
                    j++;
                    i--;
                }
                for (i = 0; i < n; i++) {
                    for (j = 0; j < n; j++)
                        System.out.printf("%4d", magicSquare[i][j]);
                    System.out.println();
                }
            }
        }
}
